#!/usr/bin/env python3
# coding: utf-8
import datetime
import logging


def _logging(**kwargs):
    level = kwargs.pop('level', None)
    filename = kwargs.pop('filename', None)
    datefmt = kwargs.pop('datefmt', None)
    format = kwargs.pop('format', None)
    if level is None:
        level = logging.DEBUG
    if filename is None:
        filename = f'default_{datetime.datetime.now().strftime("%Y%m%d")}.log'
    if datefmt is None:
        datefmt = '%Y-%m-%d %H:%M:%S'
    if format is None:
        format = '%(asctime)s - %(levelname)s - %(message)s'

    log = logging.getLogger()
    fmtter = logging.Formatter(format, datefmt)

    # 终端输出
    console = logging.StreamHandler()
    console.setFormatter(fmtter)
    console.setLevel(level)

    # 文件写入
    file_handle = logging.FileHandler(filename)
    file_handle.setFormatter(fmtter)
    file_handle.setLevel(level)

    log.addHandler(console)
    log.addHandler(file_handle)
    log.setLevel(level)
    return log


logger = _logging(level=logging.INFO)
